package ar.edu.unicen.exa.isistan.archassis.view.web;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.velocity.VelocityContext;

import ar.edu.unicen.exa.isistan.archassis.ia.api.EvalResult;
import ar.edu.unicen.exa.isistan.archassis.utils.FileUtils;
import ar.edu.unicen.exa.isistan.archassis.utils.HTMLResource;

public abstract class AbstractVelocityThread extends Thread {
	private File fileTarget;
	private EvalResult evalResult;
	
	public AbstractVelocityThread(EvalResult evalResult) {
		this.evalResult = evalResult;
		try {
			fileTarget = HTMLResource.buildLoading(getFileName());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		try {
			VelocityContext context = createContext(evalResult);
			String templateName = getTemplateName();
			
			File f = SummaryExport.getInstance().execute(templateName, context);

			FileUtils.copyFile(f, fileTarget);
			return; //todo ok
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			File onError = SummaryExport.getInstance().onError();
			FileUtils.copyFile(onError, fileTarget);
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
		
	}
	protected abstract VelocityContext createContext(EvalResult evalResult) throws Exception;
	
	protected abstract String getTemplateName();
	protected abstract String getFileName();
	
	
	public File getFile() {
		return fileTarget;
	}
	
}
